package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.WfDetail;
import com.ruoyi.system.domain.WfUser;
import com.ruoyi.system.domain.dto.WfDetailSaveDto;
import com.ruoyi.system.domain.dto.WfDetailUpdateDto;
import com.ruoyi.system.mapper.WfDetailMapper;
import com.ruoyi.system.service.IWfDetailService;
import com.ruoyi.system.service.IWfUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * WIFI信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-10
 */
@Service
public class WfDetailServiceImpl extends ServiceImpl<WfDetailMapper, WfDetail> implements IWfDetailService {
    @Resource
    private IWfUserService wfUserService;

    /**
     * 查询WIFI信息列表
     *
     * @param wfDetail WIFI信息
     * @return WIFI信息
     */
    @Override
    public List<WfDetail> selectWfDetailList(WfDetail wfDetail) {
        return list();
    }

    /**
     * 根据unionId获取Wifi列表
     *
     * @param unionId wx unionId
     * @return wifi列表
     */
    @Override
    public List<WfDetail> getWifiListByUnionId(String unionId) {
        WfUser sysUser = wfUserService.selectWfUserByOpenId(unionId);
        Assert.notNull(sysUser, "用户不存在");
        return this.list(new LambdaQueryWrapper<WfDetail>()
                .eq(WfDetail::getBelongTo, sysUser.getOpenId())
                .orderByDesc(WfDetail::getCreateTime)
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveWf(WfDetailSaveDto saveDto) {
        WfDetail wfDetail = BeanUtil.toBean(saveDto, WfDetail.class);
        if (ObjUtil.isNotEmpty(saveDto.getSubUnionId())) {
            WfUser sysUser = wfUserService.selectWfUserByOpenId(saveDto.getSubUnionId());
            if (ObjUtil.isNotEmpty(sysUser)) {
                wfDetail.setBelongTo(String.valueOf(sysUser.getOpenId()));
            }
        }
        baseMapper.insert(wfDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWfById(WfDetailUpdateDto updateDto) {
        Assert.notNull(updateDto.getWfId(), "数据ID不能为空");
        baseMapper.updateById(BeanUtil.toBean(updateDto, WfDetail.class));
    }

    @Override
    public int deleteWifi(String wfId, String openId) {
        WfDetail one = this.getOne(new LambdaQueryWrapper<WfDetail>()
                .eq(WfDetail::getBelongTo, openId)
                .eq(WfDetail::getWfId, wfId));
        if (one == null) {
            throw new ServiceException("该WIFI不在自己名下");
        }
        this.removeById(wfId);
        return 1;
    };
    //todo 还差一个归属人转移接口，一般通过二维码扫码绑定，或者分享链接点击绑定

}
